package com.zfl.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

@Component
public class RedisUtil 
{
	private   String host="127.0.0.1";
	private  Integer port=6379;
	private String password="";
	private  Jedis jredis=null;
	
 /*    private static Stack<Jedis> pools=new Stack<Jedis>();
	static
	{
	   for(int i=0;i<10;i++)
		   pools.push(new Jedis(host,port));
	}*/


	public RedisUtil()
	{
		if(jredis==null)
		{
			this.jredis=new Jedis(host,port);
			if(!(password==null||"".equals(password)))
				jredis.auth(password);
		}
	}
	
	public boolean setString(String key,String value)
	{
		if(key==null||value==null) return false;
		return "OK".equals(this.jredis.set(key, value));
	}
	
	public boolean setObjectByHash(String key, Object value)
	{
		if(key==null||value==null) return false;
		Class clz=value.getClass();
		Field[] fields=clz.getDeclaredFields();
		try{
			for (Field field : fields)
			{
				field.setAccessible(true);
				Object v=field.get(value);
				if(v==null) continue;
				this.jredis.hset(key, field.getName(), v.toString());
			}
			
		}catch(Exception e)
		{
			
		}
		return true;
	}
	
	public boolean setSingleObject(String key,Object value)
	{
		if(key==null||value==null) return false;
		JSONObject josnobject=JSONObject.fromObject(value);
		String json=josnobject.toString();
		return "OK".equals(this.jredis.set(key, json));
	}

	public boolean setList(String key,List<Object> value)
	{
		if(key==null||value==null) return false;
		JSONArray josnobject=JSONArray.fromObject(value);
		String json=josnobject.toString();
		return "OK".equals(this.jredis.set(key, json));
	}
	
	public String getString(String key)
	{
		return this.jredis.get(key);
	}

	public Object getObject(String key,Object object)
	{
		if(key==null||object==null) return null;
		String json=this.jredis.get(key);
		JSONObject jsonobject=JSONObject.fromObject(json);
		return JSONObject.toBean(jsonobject,object.getClass());
	}
	
	public List<Object> getList(String key,Object object)
	{
		if(key==null||object==null) return null;
		String json=this.jredis.get(key);
		JSONArray jsonobject=JSONArray.fromObject(json);
		List<Object> lists=new ArrayList<Object>();
		Object[] data=jsonobject.toArray();
		for (Object object2 : data) 
		{
			 JSONObject tobject=(JSONObject)object2;
			 lists.add(JSONObject.toBean(tobject,object.getClass()));
		}
		return lists;
	}
	
	public boolean removeKey(String key)
	{
		if(key==null||"".equals(key)) return false;
		return this.jredis.del(key)>0;
	}
	
	//���л�����
	public boolean saveObjectByByte(String key, Object obj)
	{
		if(key==null||obj==null) return false;
		
		return "OK".equals(this.jredis.set(key.getBytes(), toByte(obj)));
	}
	public Object getObjectFromByte(String key)
	{
		if(key==null) return false;
		return toObject(this.jredis.get(key.getBytes()));
	}
	
	
	public  byte[] toByte(Object obj) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
    	ObjectOutputStream oos = null;
		byte[] bytes = null;
		try {
			oos = new ObjectOutputStream(baos);
			oos.writeObject(obj);
			bytes = baos.toByteArray();
		} catch (IOException e1) {
			e1.printStackTrace();
		} finally {
			if(baos != null) {
				try {
					baos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(oos != null) {
				try {
					oos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		return bytes;
	}
	
	public  Object toObject(byte[] bytes) {
		ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
		ObjectInputStream ois = null;
		Object obj = null;
		try {
			ois = new ObjectInputStream(bais);
			obj = ois.readObject();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			if(bais != null) {
				try {
					bais.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(ois != null) {
				try {
					ois.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return obj;
	}

	
}
